package com.lingnan.blog.service;

import com.lingnan.blog.entity.Article;
import com.lingnan.blog.entity.User;
import com.lingnan.blog.entity.View;
import com.lingnan.blog.mapper.ArticleMapper;
import com.lingnan.blog.mapper.ViewMapper;
import com.lingnan.blog.utils.DateUtil;
import com.lingnan.blog.utils.IpUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.net.UnknownHostException;
import java.util.Date;
import java.util.List;

/**
 * 文章数据业务处理层
 *
 * @author HeGuanChao
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class ArticleService {
    @Autowired
    private ArticleMapper articleMapper;
    @Autowired
    private ViewMapper viewMapper;

    public List<Article> findAll() {
        return articleMapper.selectAll();
    }

    public Article findById(Integer id) {
        return articleMapper.selectById(id);
    }


    public List<Article> findInterviewArticle() {
        return articleMapper.selectInterviewArticle();
    }


    public void save(Article article) {
        //默认文章浏览数
        article.setViews(1);
        //默认收藏人数
        article.setCollects(0);
        //默认评论总数
        article.setComments(0);
        //文章发表时间
        article.setPutime(DateUtil.format(new Date()));
        //保存
        //保存到数据库
        articleMapper.insert(article);
    }

    public void update(Article article) {
        //更新时间
        article.setPutime(DateUtil.format(new Date()));
        articleMapper.update(article);
    }

    public void updateView(int id, User user) throws UnknownHostException {
        if (user != null) {
            //如果用户登录了
            //获取用户的id
            String uId = String.valueOf(user.getId());
            //根据用户id和文章id查询浏览记录
            View view = viewMapper.selectViewByIpAndAid(id, uId);
            if (view == null) {
                //记录为空，插入新的浏览记录
                viewMapper.insertSelective(new View(null, uId, id,
                        DateUtil.format(new Date())));
                //更新文章的浏览数
                articleMapper.updateView(id);
            }
        } else {
            //如果用户没登录
            //获取当前访问的用户ip
            String ip = IpUtil.getHostAddress();
            //根据当前访问的用户ip查询浏览记录
            View view = viewMapper.selectViewByIpAndAid(id, ip);
            if (view == null) {
                //记录为空，插入新的浏览记录
                viewMapper.insertSelective(new View(null, ip, id,
                        DateUtil.format(new Date())));
                //更新文章的浏览数
                articleMapper.updateView(id);
            }
        }
    }

    public int findByViews(Integer id) {
        return articleMapper.selectByViews(id);
    }

    public Integer findByCollects(Integer id) {
        return articleMapper.findByCollects(id);
    }

    public Integer updateCollects(Integer id, Integer collects) {
        return articleMapper.updateCollects(id, collects);
    }

    public List<Article> findByTag(String tag) {
        return articleMapper.selectByTag(tag);
    }


    public void updateComments(Integer id) {
        articleMapper.updateComments(id);
    }
}
